<template>
    <div class="user-information-container">
        <div class="information-header">
            <div>
                <p style="padding: 10px 50px 10px ;text-align: left;font-weight:bold">个人资料</p>
            </div>
        </div>
        <div class="information-content">
            <div>
                <el-row>
                    <el-col :span="12">
                        <el-form ref="userInfoForm" :model="userInfoForm" label-width="100px">
                            <el-form-item label="用户名" prop="username">
                                <el-input size="large" type="text" :placeholder="nowUser.username" :disabled="true">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="邮箱" prop="email">
                                <el-input size="large" type="email" :placeholder="nowUser.email" :disabled="true">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="联系电话" prop="phone">
                                <el-input size="large" type="string" v-model="userInfoForm.phone" placeholder="联系电话" :disabled="!canSet">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="微信" prop="wechat">
                                <el-input size="large" type="string" v-model="userInfoForm.wechat" placeholder="微信" :disabled="!canSet">
                                </el-input>
                            </el-form-item>
                            <el-form-item label="地址" prop="address">
                                <el-input size="large" type="string" v-model="userInfoForm.address" placeholder="地址" :disabled="!canSet">
                                </el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button v-if="canSet" size="large" type="primary" @click="addUserInfo">提交</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                </el-row>
            </div>
        </div>
    </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'
import userinfoService from 'services/userinfoService.js'
import * as notifyUtil from 'utils/notifyUtil.js'

export default {
  components: {},
  computed: mapGetters({
    nowUser: 'nowUser'
  }),
  data() {
    return {
      canSet: false,
      userInfoForm: {
        phone: '',
        wechat: '',
        address: ''
      }
    }
  },
  methods: {
    ...mapActions({
      setNowUser: 'setNowUser'
    }),
    addUserInfo() {
      let userInfo = {
        userid: this.nowUser.id,
        phone: parseInt(this.userInfoForm.phone),
        wechat: this.userInfoForm.wechat,
        address: this.userInfoForm.address
      }
      userinfoService.addUserInfo(userInfo).then(response => {
        if (response.data.id === null || response.data.id === undefined) {
          notifyUtil.showError('更新用户信息', '失败，服务器错误')
        } else {
          notifyUtil.showSuccess('更新用户信息', '成功')
        }
      })
    },
    getUserInfo() {
      userinfoService.getUserInfo(this.nowUser.id).then(response => {
        if (response.data.Userinfo === null || response.data.Userinfo === undefined) {
          this.$set(this, 'canSet', true)
          notifyUtil.showError('获取用户信息失败', '请填写用户信息')
        } else {
          this.$set(this.userInfoForm, 'phone', response.data.Userinfo[0].phone)
          this.$set(this.userInfoForm, 'address', response.data.Userinfo[0].address)
          this.$set(this.userInfoForm, 'wechat', response.data.Userinfo[0].wechat)
        }
      })
    }
  },
  mounted() {
    this.getUserInfo()
  }
}
</script>

<style scoped>
.user-information-container {
  margin-top: 50px;
  width: 1070px;
  height: 100%;
  float: left;
  /* text-align: left; */
  /* border: 1px solid #DCDFE6; */
  background-color: #ffffff;
}
.photo-holder {
  text-align: left;
  position: relative;
  float: right;
  height: 100%;
  width: 100%;
}
.photo-holder .span {
  text-align: left;
  position: relative;
  float: left;
}
</style>